package com.mikao.service.impl;

import com.mikao.dao.UserBankDao;
import com.mikao.dao.UserDao;
import com.mikao.dao.WithdrawDao;
import com.mikao.dto.bo.ReqDataTableDto;
import com.mikao.dto.vo.RspDataTableDto;
import com.mikao.dto.vo.WithdrawDto;
import com.mikao.entity.MoneyDetail;
import com.mikao.entity.User;
import com.mikao.entity.UserBank;
import com.mikao.service.WithdrawService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class WithdrawServiceImpl implements WithdrawService {

    private WithdrawDao withdrawDao;
    private UserDao userDao;
    private UserBankDao userBankDao;

    public WithdrawServiceImpl(WithdrawDao withdrawDao, UserDao userDao, UserBankDao userBankDao) {
        this.withdrawDao = withdrawDao;
        this.userDao = userDao;
        this.userBankDao = userBankDao;
    }

    @Override
    public RspDataTableDto<WithdrawDto> withdrawList(ReqDataTableDto dataTableDTO) {
        WithdrawDto dto = new WithdrawDto();
        List<WithdrawDto> dtos = new ArrayList<>();
        List<MoneyDetail> list = withdrawDao.findAllByIntroduceAndTypeOrderByCreateTimeDesc("银行卡提现", 1);
        if (!ObjectUtils.isEmpty(list)) {
            for (MoneyDetail detail : list) {
                BeanUtils.copyProperties(detail, dto);
                User user = userDao.findOne(detail.getUserId());
                if (!ObjectUtils.isEmpty(user)) {
                    dto.setName(user.getName());
                    UserBank bank = userBankDao.findAllByUserId(user.getId());
                    if (!ObjectUtils.isEmpty(bank)) {
                        dto.setBankCode(bank.getBankCode());
                        dto.setBankName(bank.getBankName());
                    }
                    dtos.add(dto);
                }
            }
        }
        return new RspDataTableDto<>(dataTableDTO.getDraw(), (long) dtos.size(), dtos);
    }
}
